package com.ws.test;

/**
 * @author 王硕
 * @version jdk21
 * @className SharedBikeCompany
 * @description
 * @date 2024/11/13 16:34
 */
import java.text.SimpleDateFormat;
import java.util.Date;

public class SharedBikeCompany {
    private Integer bikeCompanyId;
    private String bikeCompanyName;
    private SharedBike[] sharedBikes; // 公司持有共享单车
    private Integer sum; // 公司单车总量
    private Integer count; // 公司单车借出次数
    private Integer borrowCount; // 单车借出次数

    public SharedBikeCompany(String bikeCompanyName, int capacity) {
        this.bikeCompanyName = bikeCompanyName;
        this.sharedBikes = new SharedBike[capacity];
        this.sum = 0;
        this.count = 0;
        this.borrowCount = 0;
    }

    // Getters
    public SharedBike[] getSharedBikes() { return sharedBikes; }
    public Integer getBikeCompanyId() { return bikeCompanyId; }
    public String getBikeCompanyName() { return bikeCompanyName; }
    public Integer getSum() { return sum; }
    public Integer getCount() { return count; }
    public Integer getBorrowCount() { return borrowCount; }
    public void setBorrowCount(Integer borrowCount) { this.borrowCount = borrowCount; }

    // Methods
    public void addBike(SharedBike bike) {
        if (sum < sharedBikes.length) {
            sharedBikes[sum++] = bike;
        } else {
            System.out.println("没有更多空间投放新的Bike");
        }
    }

    public boolean removeBike(Integer bid) {
        for (int i = 0; i < sum; i++) {
            if (sharedBikes[i] != null && sharedBikes[i].getBid().equals(bid)) {
                if (sharedBikes[i].getStatus() == 0) {
                    System.out.println("不能删除已借出的Bike");
                    return false;
                } else {
                    for (int j = i; j < sum - 1; j++) {
                        sharedBikes[j] = sharedBikes[j + 1];
                    }
                    sharedBikes[--sum] = null;
                    return true;
                }
            }
        }
        System.out.println("没有找到编号为" + bid + "的Bike");
        return false;
    }

    public void borrowBike(int index, String borrowTime) {
        if (index >= 0 && index < sum && sharedBikes[index].getStatus() == 1) {
            sharedBikes[index].setStatus(0);
            sharedBikes[index].setBorrowTime(borrowTime);
            count++;
            borrowCount++;
        } else {
            System.out.println("Bike不可借或不存在");
        }
    }

    public void returnBike(int index, String returnTime) {
        if (index >= 0 && index < sum && sharedBikes[index].getStatus() == 0) {
            sharedBikes[index].setStatus(1);
            sharedBikes[index].setBorrowTime(null);
            calculateCharge(index, returnTime);
        } else {
            System.out.println("Bike未借出或不存在");
        }
    }

    private void calculateCharge(int index, String returnTime) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = sd.parse(sharedBikes[index].getBorrowTime());
            Date d2 = sd.parse(returnTime);
            long diff = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000); // 计算日期差
            System.out.println("租金为: " + diff + "元");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
